package com.studio49.exmple.core.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.studio49.exmple.common.constant.ForwardConstant;
import com.studio49.exmple.common.util.PubUtils;
import com.studio49.exmple.entity.web.sys.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class LoginInterceptor extends HandlerInterceptorAdapter{
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//		Object user = request.getSession().getAttribute("user");

		User user = PubUtils.getSessionUser(request);
		if (user == null) {
			//未登录，返回登录页
			request.setAttribute("msg", "没有权限，请先登录！");
//			request.getRequestDispatcher(ForwardConstant.LOGIN_H + ForwardConstant.LOGIN_PAGE).forward(request, response);
			response.sendRedirect(ForwardConstant.LOGIN_H + ForwardConstant.LOGIN_PAGE);
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) {
		// controller方法处理完毕后，调用此方法
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		// 页面渲染完毕后调用此方法
	}

}
